package controller.menubar.commands;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import model.GlobalParams;
import view.MainFrame;

	
public class Delete implements ActionListener , PropertyChangeListener {
	
	private JProgressBar progressBar;
	private JButton startButton;
	private JTextField taskOutput;
	private DeleteFile task;
	private MainFrame frame;
	private JFrame newFrame;

	public Delete(MainFrame frame) {			
		this.frame = frame;			
	}
	
	public void Configure() {
		
		newFrame = new JFrame(GlobalParams.getInstance().stringCollection.getOther_operation_manager());
		newFrame.setLayout(new BorderLayout());
		
		startButton = new JButton("Remove");
		startButton.setActionCommand("start");
		startButton.addActionListener(new ActionListener() {			
			@Override
			public void actionPerformed(ActionEvent arg0) {				
				String confirm_question = GlobalParams.getInstance().stringCollection.getOther_confirm_question();		
				String confirm_titlewindow = GlobalParams.getInstance().stringCollection.getOther_confirm_title();			
			    int response = JOptionPane.showConfirmDialog(null, confirm_question, confirm_titlewindow,
			    JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
			    
			    if (response == JOptionPane.YES_OPTION) 
			    {			    	
				     task.execute();				     	
			    } 
			    else 
			    {
			    	newFrame.dispose();				    	
			    }				
			}
		});
		
		progressBar = new JProgressBar(0, 100);
		progressBar.setValue(0);
		progressBar.setStringPainted(true);
		
		
		taskOutput = new JTextField();		
		//taskOutput.setMargin(new Insets(5,5,5,5));
		//taskOutput.setEditable(false);
		
		JPanel panel = new JPanel();
		panel.add(startButton);
		panel.add(progressBar);
		
		newFrame.add(panel, BorderLayout.PAGE_START);
		newFrame.add(new JScrollPane(taskOutput), BorderLayout.CENTER);			
		newFrame.setMinimumSize(new Dimension(400,100));
		newFrame.setLocationRelativeTo(frame);
		newFrame.pack();
		newFrame.setVisible(true);
	
	}
	
	
	public void actionPerformed(ActionEvent evt) {			
		if (frame.getMainPanel().getPrimary().getDiskContent().getSelectedFile() != null ) 
		{				
			File from =  frame.getMainPanel().getPrimary().getDiskContent().getSelectedFile();			
			if (from.canWrite()) {
				Configure();
				task = new DeleteFile(from , frame);
				task.addPropertyChangeListener(this);							
				taskOutput.setText(from.getAbsolutePath() + "\n");
			} else {				
				String op = GlobalParams.getInstance().stringCollection.getOther_write_permission();				 
				JOptionPane.showConfirmDialog(null, op, "Error", JOptionPane.ERROR_MESSAGE);					
			}
		}
	}		
	
	public void propertyChange(PropertyChangeEvent evt) {
		if ("progress" == evt.getPropertyName()) {
			int progress = (Integer) evt.getNewValue();
			progressBar.setValue(progress);			
		} 
	}	
		
}	